﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CanDoo;
using CanDoo.Core;
using CanDoo.Core.Data;
using CanDoo.Data;
using CanDoo.Oil.Core.DTOs;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.Models;

namespace CanDoo.Oil.Core.Services
{
    /// <summary>
    /// 业务实现——库存
    /// </summary>
    public class StockService : CanDoo.Data.Entity.Services.ServiceBase<Stock>, IStockContract
    {
        public StockService()
        {
        
        }

        public OperationResult InToStore(Warehouse warehouse,List<ProductItem> productItemList,bool CalculateCost)
        {
            var result = new OperationResult(OperationResultType.NoChanged);
            var addList = new List<Stock>();
            var editList = new List<Stock>();
            
            foreach (var m in productItemList)
            {
                var isNew = false;
                var mEntity = FirstOrDefault(p => p.ProductId == m.ProductId && p.WarehouseId==warehouse.Id);
                if (mEntity == null)
                {
                    mEntity = new Stock();
                    isNew = true;
                }

                mEntity.Name = m.Name+"["+m.Model+" "+m.Specifications + "]";
                mEntity.WarehouseId = warehouse.Id;
                mEntity.ProductId = m.ProductId;
                if (isNew)
                {
                    if (CalculateCost)
                    {
                        mEntity.HighPrice = m.Price.ToDouble();
                        mEntity.LowPrice = m.Price.ToDouble();
                        mEntity.CostPrice = m.Price.ToDouble();
                        mEntity.NowPrice = m.Price.ToDouble();
                    }
                    else
                    {
                        mEntity.HighPrice = 0;
                        mEntity.LowPrice = 0;
                        mEntity.CostPrice = 0;
                        mEntity.NowPrice = 0;
                    }

                    mEntity.StockNum = m.Num.ToDouble();

                    mEntity.OnWayNum = 0;
                    mEntity.WaitBackNum = 0;

                    mEntity.EnableAlerm = false;
                    mEntity.ReasonableNum = 0;
                    mEntity.TopNum = 0;
                    mEntity.BottomNum = 0;
                }
                else
                {
                    if (CalculateCost)
                    {
                        if (m.Price > mEntity.HighPrice)
                            mEntity.HighPrice = m.Price.ToDouble();

                        if (m.Price > 0 && m.Price < mEntity.LowPrice)
                            mEntity.LowPrice = m.Price.ToDouble();

                        if ((mEntity.StockNum + m.Num.ToDouble()) > 0)
                            mEntity.CostPrice = Math.Round((m.Price.ToDouble() * m.Num.ToDouble() + mEntity.StockNum * mEntity.CostPrice) / (m.Num.ToDouble() + mEntity.StockNum),5);
                        else
                            mEntity.CostPrice = 0;

                        mEntity.NowPrice = m.Price.ToDouble();
                    }

                    mEntity.StockNum = mEntity.StockNum + m.Num.ToDouble();

                    mEntity.OnWayNum = 0;
                    mEntity.WaitBackNum = 0;
                }

                mEntity.Remark = "";

                if (isNew)
                {
                    mEntity.Id = IdMaker.NewId();
                    addList.Add(mEntity);
                }
                else
                    editList.Add(mEntity);
            }
            if(addList.Count>0)
            AddEntity(addList);
            if(editList.Count>0)
            UpdateEntity(editList);

            return result;
        }


        public OperationResult OutFromStore(Warehouse warehouse, List<ProductItem> productItemList, bool CalculateCost)
        {
            var result = new OperationResult(OperationResultType.NoChanged);
            var editList = new List<Stock>();

            foreach (var m in productItemList)
            {
                var mEntity = FirstOrDefault(p => p.ProductId == m.ProductId && p.WarehouseId == warehouse.Id);

                mEntity.Name = m.Name + "[" + m.Model + " " + m.Specifications + "]";

                if (mEntity.CostPrice < 0)
                    mEntity.CostPrice = 0;
                
                if (CalculateCost)
                {
                    if ((mEntity.StockNum + m.Num.ToDouble() * -1) <= 0)
                        mEntity.CostPrice = 0;
                    else
                        mEntity.CostPrice = Math.Round((m.Price.ToDouble() * m.Num.ToDouble() * -1 + mEntity.StockNum * mEntity.CostPrice) / (m.Num.ToDouble() * -1 + mEntity.StockNum),5);
                }

                mEntity.StockNum = mEntity.StockNum + m.Num.ToDouble() * -1;

                mEntity.OnWayNum = 0;
                mEntity.WaitBackNum = 0;
                mEntity.Remark = "";
                editList.Add(mEntity);
            }
            if (editList.Count > 0)
                UpdateEntity(editList);

            return result;
        }
    }
}
